<?php
class Default_Model_ThanhPhoMapper {
	
	protected $_dbTable;
	
	public function setDbTable($dbTable) {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable() {
        if (null === $this->_dbTable) {
            $this->setDbTable('Default_Model_DbTable_ThanhPho');
        }
        return $this->_dbTable;
    }
    
	public function fetch($id) {
		$db = Zend_Registry::get('connectDB');
		
		$stmt = $db->query("CALL tb_thanh_pho_GetItem(?)", array($id));
		$result = $this->processResultSet($stmt->fetchAll());
		foreach ($result as $r) {
			return $r;
		}
    }

    public function fetchAll($query = null) {
		$db = Zend_Registry::get('connectDB');
		
        if ($query == null) {
            $stmt = $db->query("CALL tb_thanh_pho_GetItem(?)", array(0));
        } else {
            $stmt = $this->getDbTable()->fetchAll($query);
        }
        return $this->processResultSet($stmt->fetchAll());
    }

    private function processResultSet($resultSet) {
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Default_Model_TableColumn_ThanhPho();
            $entry->setId($row['id']);
            $entry->setTen($row['ten']);
            $entries[] = $entry;
        }
        return $entries;
    }
}